package cn.iocoder.yudao.module.member.service.pickupinfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.app.pickupinfo.vo.PickupInfoPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.pickupinfo.vo.PickupInfoSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.pickupinfo.PickupInfoDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.pickupinfo.PickupInfoMapper;
import cn.iocoder.yudao.module.member.utils.AuthIdentityUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.PICKUP_INFO_DUPLICATE;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.PICKUP_INFO_NOT_EXISTS;

/**
 * 用户提料人信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class PickupInfoServiceImpl implements PickupInfoService {

    @Resource
    private PickupInfoMapper pickupInfoMapper;
    @Resource
    private AuthIdentityUtil authIdentityUtil;

    @Override
    public Long createPickupInfo(Long userId, PickupInfoSaveReqVO createReqVO) {
        // 插入
        PickupInfoDO pickupInfo = BeanUtils.toBean(createReqVO, PickupInfoDO.class);
        // TODO: 2025/4/26 身份证核验
        PickupInfoDO pickupInfoDO = pickupInfoMapper.selectOne(PickupInfoDO::getIdNo, pickupInfo.getIdNo());
        if (pickupInfoDO != null) {
            pickupInfo.setId(pickupInfoDO.getId());
        }
        long count = pickupInfoMapper.selectCountByUserAndIdNo(userId, createReqVO.getIdNo());
        if (count > 1) {
            throw exception(PICKUP_INFO_DUPLICATE);
        }
        // 如果添加的是默认，则将原默认修改为非默认
        if (Boolean.TRUE.equals(createReqVO.getDefaultStatus())) {
            List<PickupInfoDO> addresses = pickupInfoMapper.selectListByUserIdAndDefaulted(userId, true);
            addresses.forEach(address -> pickupInfoMapper.updateById(new PickupInfoDO().setId(address.getId()).setDefaultStatus(false)));
        }

        pickupInfo.setUserId(userId);
        pickupInfoMapper.insertOrUpdate(pickupInfo);
        // 返回
        return pickupInfo.getId();
    }

    @Override
    public void updatePickupInfo(PickupInfoSaveReqVO updateReqVO) {
        // 校验存在
        validatePickupInfoExists(updateReqVO.getId());
        long count = pickupInfoMapper.selectCountByUserAndIdNo(updateReqVO.getUserId(), updateReqVO.getName());
        if (count > 1) {
            throw exception(PICKUP_INFO_DUPLICATE);
        }
        // 更新
        PickupInfoDO updateObj = BeanUtils.toBean(updateReqVO, PickupInfoDO.class);

        // 如果添加的是默认，则将原默认修改为非默认
        if (Boolean.TRUE.equals(updateReqVO.getDefaultStatus())) {
            List<PickupInfoDO> addresses = pickupInfoMapper.selectListByUserIdAndDefaulted(updateReqVO.getUserId(), true);
            addresses.forEach(address -> pickupInfoMapper.updateById(new PickupInfoDO().setId(address.getId()).setDefaultStatus(false)));
        }
        pickupInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePickupInfo(Long id) {
        // 校验存在
        validatePickupInfoExists(id);
        // 删除
        pickupInfoMapper.deleteById(id);
    }
    @Override
    public PickupInfoDO validatePickupInfoExists(Long id) {
        PickupInfoDO pickupInfoDO = pickupInfoMapper.selectById(id);
        if (pickupInfoDO == null) {
            throw exception(PICKUP_INFO_NOT_EXISTS);
        }
        return pickupInfoDO;
    }

    @Override
    public PickupInfoDO getPickupInfo(Long id) {
        return pickupInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PickupInfoDO> getPickupInfoPage(PickupInfoPageReqVO pageReqVO) {
        return pickupInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public void createPickupInfoByUser(MemberUserDO user) {
        PickupInfoSaveReqVO reqVO = new PickupInfoSaveReqVO();
        reqVO.setName(user.getName());
        reqVO.setMobile(user.getMobile());
        reqVO.setIdNo(user.getIdentityId());
        List<String> pics = new ArrayList<>(2);
        pics.add(user.getIdentityPicFont());
        pics.add(user.getIdentityPicBack());
        reqVO.setIdCardPic(pics);
        reqVO.setDefaultStatus(true);
        createPickupInfo(user.getId(), reqVO);
    }

    @Override
    public PickupInfoDO getDefaultPickupInfo(Long userId) {
        List<PickupInfoDO> infos = pickupInfoMapper.selectListByUserIdAndDefaulted(userId, true);
        return CollUtil.getFirst(infos);
    }

    @Override
    public List<PickupInfoDO> getPickupInfoList(Long userId) {
        return pickupInfoMapper.selectListByUserIdAndDefaulted(userId, null);
    }

}